//
//  HomeViewController.m
//  futureLive
//
//  Created by zhoubiwen on 15/11/17.
//  Copyright © 2015年 ZHOU_LI_SUN. All rights reserved.
//

#import "HomeViewController.h"
#import "NetWorkManager.h"
#import "HomeModel.h"
#import "HomeOneImageCell.h"
#import "ThreeImagesEqualCell.h"
#import "ThreeImageCell.h"
#import "DetailsViewController.h"

@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource,ThreeImagesEqualCellDelegate,ThreeImageCellDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableView;


@property (nonatomic, strong) NSMutableArray *sourceArray; // 数组中存放homeModel

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self initSubViews];
    
}
/**
 *  初始化数据
 */
- (void)initData {
    
 [NetWorkManager getJsonDataWithURL:homeURL success:^(id data) {

     self.sourceArray = [NSMutableArray new];
     for (NSDictionary *dataDict in data[@"data"]) {
         
         HomeModel *model = [[HomeModel alloc]init];
         [model setValuesForKeysWithDictionary:dataDict];
         [self.sourceArray addObject:model];
     }
      [self.tableView reloadData];
     
 } fail:^{
     NSLog(@"网络数据请求失败.");
 }];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
/**
 *  初始化子视图
 */

- (void)initSubViews {
    
    
}

#pragma mark - ThreeImagesEqualCellDelegate
- (void)didClickImageIndex:(NSUInteger)index withHomeModel:(HomeModel *)homeModel {
    HomeImageModel *model = homeModel.list[index];
    if ([homeModel.type intValue] == 3) {
        DetailsViewController *detailsVC = [DetailsViewController new];
        detailsVC.headImageURL = model.img;
        detailsVC.value = model.value;
        [self.navigationController pushViewController:detailsVC animated:YES];

    }
    
}

#pragma mark - ThreeImageCellDelegate
- (void)didClickThreeImageCellWithIndex:(NSUInteger)index homeModel:(HomeModel *)homeModel {
    HomeImageModel *model = homeModel.list[index];
    
    if ([homeModel.type intValue] == 3) {
        DetailsViewController *detailsVC = [DetailsViewController new];
        detailsVC.value = model.value;
        detailsVC.headImageURL = model.img;
        [self.navigationController pushViewController:detailsVC animated:YES];
        
    }

}
#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    HomeModel *model = self.sourceArray[indexPath.row];
    
    if ([model.lotType integerValue] == 3) {
        
        HomeOneImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oneImageCell" forIndexPath:indexPath];
        cell.homeModel = model;
        
        return cell;
        
    }
    if ([model.lotType integerValue] == 2) {
        
        ThreeImagesEqualCell *cell = [tableView dequeueReusableCellWithIdentifier:@"threeImageEqualCell" forIndexPath:indexPath];
        cell.homeModel = model;
        cell.delegate = self;
        return cell;
    }
    
    if ([model.lotType integerValue] == 4) {
        ThreeImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThreeImageCell" forIndexPath:indexPath];
        cell.homeModel = model;
        cell.delegate = self;
        return cell;
    }
    
    return nil;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    HomeModel *homeModel = self.sourceArray[indexPath.row];
    HomeImageModel *model = homeModel.list.firstObject;
    if ([homeModel.type intValue] == 3) {
        DetailsViewController *detailsVC = [DetailsViewController new];
        detailsVC.value = model.value;
        detailsVC.headImageURL = model.img;
        [self.navigationController pushViewController:detailsVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
